﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DevExpress.Web.ASPxEditors;

public partial class Store_Keeper_SK_Purchase_Order : System.Web.UI.Page
{
    public static int indexx;

    public static Boolean result1;
    List<int> columset = new List<int>();

    protected void Page_Load(object sender, EventArgs e)
    {
        //Panel3.Visible = false;
        ASPxButton1.Enabled = true;
        int i = Bestlife.ERP.BusinessRules.B_PurchaseOrder.getlastGRN();
        ASPxTextBox1.Text = i.ToString();
        Session["current"] = "GRN Request";//current page

        if (Session["user"] != null)//check user
        {
            string user = Session["user"].ToString();
            if (user == "viewer")
            {
                Response.Redirect("index.aspx");
            }

        }
        else
        {
            Response.Redirect("index.aspx");
        }

        if (!IsPostBack && !IsCallback)
            DropDownList1.SelectedIndex = 0;
        if (!Page.IsPostBack)//refresh
        {
            SetInitialRow();
            ViewState["index"] = 0;
            ViewState["total"] = 0;
            ViewState["row"] = 0;
            setprice(0);

            DropDownList box1 = (DropDownList)Gridview1.Rows[0].Cells[1].FindControl("DropDownList3");

            string brand = Bestlife.ERP.BusinessRules.B_GRN.select_product(box1.SelectedValue);
            TextBox box8 = (TextBox)Gridview1.Rows[0].Cells[3].FindControl("TextBox8");
            box8.Text = brand;
            TextBox box7 = (TextBox)Gridview1.Rows[0].Cells[3].FindControl("TextBox7");
            box7.Text = box1.Text;


        }



    }
    private void SetInitialRow()
    {
        try
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            //dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));

            dt.Columns.Add(new DataColumn("DropDownList3", typeof(string)));
            dt.Columns.Add(new DataColumn("DropDownList4", typeof(string)));
            dt.Columns.Add(new DataColumn("Column2", typeof(string)));
            dt.Columns.Add(new DataColumn("Column3", typeof(string)));
            dt.Columns.Add(new DataColumn("Column4", typeof(string)));
            dt.Columns.Add(new DataColumn("Column5", typeof(string)));
            dt.Columns.Add(new DataColumn("Column6", typeof(string)));
            dt.Columns.Add(new DataColumn("Column7", typeof(string)));
            dt.Columns.Add(new DataColumn("Column8", typeof(string)));


            dr = dt.NewRow();
            //dr["RowNumber"] ="1";
            dr["DropDownList3"] = string.Empty;
            dr["DropDownList4"] = string.Empty;
            dr["Column2"] = string.Empty;
            dr["Column3"] = string.Empty;
            dr["Column4"] = string.Empty;
            dr["Column5"] = string.Empty;
            dr["Column6"] = string.Empty;
            dr["Column7"] = string.Empty;
            dr["Column8"] = string.Empty;

            dt.Rows.Add(dr);
            //dr = dt.NewRow();

            //Store the DataTable in ViewState
            ViewState["CurrentTable"] = dt;
            DataColumn[] dcKey = new DataColumn[1];

            //dcKey[0] = dt.Columns["RowNumber"];
            //dt.PrimaryKey = dcKey;
            Gridview1.DataSource = dt;
            Gridview1.DataBind();
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }//initial table
    private void AddNewRowToGrid()//add new row to table
    {
        try
        {

            int rowIndex = 0;

            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        //extract the TextBox values

                        //ASPxTextBox box0 = (ASPxTextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("RowNumber");
                        DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList3");
                        DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList4");
                        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox2");
                        TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox3");
                        TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox4");
                        TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox5");
                        TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox6");
                        TextBox box8 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox7");
                        TextBox box9 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox8");

                        drCurrentRow = dtCurrentTable.NewRow();



                        //dtCurrentTable.Rows[i - 1]["RowNumber"] = i + 1;
                        dtCurrentTable.Rows[i - 1]["DropDownList3"] = box1.Text;
                        dtCurrentTable.Rows[i - 1]["DropDownList4"] = box2.Text;
                        dtCurrentTable.Rows[i - 1]["Column2"] = box3.Text;
                        dtCurrentTable.Rows[i - 1]["Column3"] = box4.Text;
                        dtCurrentTable.Rows[i - 1]["Column4"] = box5.Text;
                        dtCurrentTable.Rows[i - 1]["Column5"] = box6.Text;
                        dtCurrentTable.Rows[i - 1]["Column6"] = box7.Text;
                        dtCurrentTable.Rows[i - 1]["Column7"] = box7.Text;
                        dtCurrentTable.Rows[i - 1]["Column8"] = box7.Text;


                        rowIndex++;

                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;

                    ViewState["index"] = dtCurrentTable.Rows.Count - 1;
                    Gridview1.DataSource = dtCurrentTable;
                    Gridview1.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }

            //Set Previous Data on Postbacks
            SetPreviousData();
        }
        catch
        {

        }
    }
    private void SetPreviousData()
    {
        try
        {
            int rowIndex = 0;
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrentTable"];

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {

                        DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList3");
                        DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList4");
                        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox2");
                        TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox3");
                        TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox4");
                        TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox5");
                        TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox6");
                        TextBox box8 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox7");
                        TextBox box9 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox8");
                        //if (Session["addrow"] != null)
                        //{


                        box1.Text = dt.Rows[i]["DropDownList3"].ToString();
                        box2.Text = dt.Rows[i]["DropDownList4"].ToString();
                        box3.Text = dt.Rows[i]["Column2"].ToString();
                        box4.Text = dt.Rows[i]["Column3"].ToString();
                        box5.Text = dt.Rows[i]["Column4"].ToString();
                        box6.Text = dt.Rows[i]["Column5"].ToString();
                        box7.Text = dt.Rows[i]["Column6"].ToString();
                        box8.Text = dt.Rows[i]["Column7"].ToString();
                        box9.Text = dt.Rows[i]["Column8"].ToString();
                        string brand = Bestlife.ERP.BusinessRules.B_GRN.select_product(box1.SelectedValue);
                        box9.Text = brand;
                        box8.Text = box1.SelectedValue;

                        //box1.SelectedIndex = -1;
                        //box2.SelectedIndex = -1;
                        //box3.ReadOnly = true;
                        //box4.ReadOnly = true;
                        //box4.ReadOnly = true;
                        //box5.ReadOnly = true;
                        //box6.ReadOnly = true;
                        //box7.ReadOnly = true;

                        //}
                        box1.Enabled = false;
                        box2.Enabled = false;
                        box3.Enabled = false;
                        box4.Enabled = false;
                        box5.Enabled = false;
                        box6.Enabled = false;
                        box7.Enabled = true;
                        box8.Enabled = false;
                        box9.Enabled = false;
                        //Gridview1.Rows[i].Enabled = false;
                        rowIndex++;

                    }

                    DropDownList box01 = (DropDownList)Gridview1.Rows[rowIndex - 1].Cells[1].FindControl("DropDownList3");
                    DropDownList box02 = (DropDownList)Gridview1.Rows[rowIndex - 1].Cells[2].FindControl("DropDownList4");
                    TextBox box03 = (TextBox)Gridview1.Rows[rowIndex - 1].Cells[3].FindControl("TextBox2");
                    TextBox box04 = (TextBox)Gridview1.Rows[rowIndex - 1].Cells[4].FindControl("TextBox3");
                    TextBox box05 = (TextBox)Gridview1.Rows[rowIndex - 1].Cells[5].FindControl("TextBox4");
                    TextBox box06 = (TextBox)Gridview1.Rows[rowIndex - 1].Cells[6].FindControl("TextBox5");
                    TextBox box07 = (TextBox)Gridview1.Rows[rowIndex - 1].Cells[6].FindControl("TextBox6");
                    TextBox box08 = (TextBox)Gridview1.Rows[rowIndex - 1].Cells[6].FindControl("TextBox7");
                    TextBox box09 = (TextBox)Gridview1.Rows[rowIndex - 1].Cells[6].FindControl("TextBox8");

                    box01.Enabled = true;
                    box02.Enabled = true;
                    box03.Enabled = true;
                    box04.Enabled = true;
                    box05.Enabled = true;
                    box06.Enabled = true;
                    box07.Enabled = true;
                    box08.Enabled = true;
                    box09.Enabled = true;

                    //Gridview1.Rows[rowIndex-1].Enabled = true;

                    ViewState["index"] = rowIndex - 1;




                }

            }

        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }//set previous data
    protected void ASPxGridView1_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
    {
        try
        {
            if (e.Column.FieldName == "Total")
            {
                decimal price = Convert.ToDecimal(e.GetListSourceFieldValue("Pur_Price"));
                int qty = Convert.ToInt32(e.GetListSourceFieldValue("Qty"));
                decimal tot = price * qty;
                e.Value = tot;
            }
            if (e.Column.FieldName == "Pur_Price")
            {
                string pro_name = Convert.ToString(e.GetListSourceFieldValue("Pro_Name"));
                decimal price = Bestlife.ERP.BusinessRules.productBV_PV.productprice(pro_name);
                e.Value = price;
            }

        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }//calculate total and unit price
    protected void ButtonAdd_Click1(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)ViewState["CurrentTable"];
        int rowIndex = dt.Rows.Count - 1;
        DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList3");
        DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList4");
        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox2");
        TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox3");
        TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox4");
        TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox5");
        TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox6");
        TextBox box8 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox7");
        TextBox box9 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox8");

        if (box4.Text == "" || box5.Text == "" || box6.Text == "")
        {

            ASPxLabel3.Visible = true;
            ASPxLabel3.Text = "You must enter a value for all required fields ";




        }
        else
        {
            setprice(0);
            AddNewRowToGrid();
            Session["addrow"] = 1;



        }





    }//row adding
    protected void ButtonAdd_Click2(object sender, EventArgs e)
    {
        try
        {
            Gridview1.EditIndex = -1;
            Gridview1.DataBind();
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }
    protected void DropDownList4_SelectedIndexChanged1(object sender, EventArgs e)
    {
        try
        {
            int index = int.Parse(ViewState["index"].ToString());
            setprice(index);

        }
        catch
        {
            Response.Redirect("404.ASPX");
        }

    }
    protected void DropDownList3_SelectedIndexChanged1(object sender, EventArgs e)
    {
        try
        {
            int row = int.Parse(ViewState["row"].ToString());
            setstoreid(row);


        }
        catch
        {

        }
    }
    public void setstoreid(int row)
    {
        try
        {

            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            int i = dtCurrentTable.Rows.Count - 1;
            DropDownList pro = (DropDownList)Gridview1.Rows[i].FindControl("DropDownList3");
            string proid = pro.Text;
            string brand = Bestlife.ERP.BusinessRules.B_GRN.select_product(proid);
            TextBox box8 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("TextBox8");
            box8.Text = brand;
            TextBox box7 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("TextBox7");
            box7.Text = proid;
            List<string> stockid = Bestlife.ERP.BusinessRules.B_PurchaseOrder.stockid(proid);
            if (stockid.Count == 0)
            {

            }
            DropDownList box2 = (DropDownList)Gridview1.Rows[i].FindControl("DropDownList4");


            box2.Items.Clear();
            string value = box2.SelectedValue;


            foreach (object selectitemid in stockid)
            {

                if (!value.Equals(selectitemid.ToString()))
                {


                    box2.Text = selectitemid.ToString();

                }
                else
                {

                }
            }
            string stockidnew = stockid[0].ToString();
            DropDownList box21 = (DropDownList)Gridview1.Rows[i].FindControl("DropDownList4");
            box21.Items.Clear();
            box21.Text = stockidnew;
            setpricenew(i, stockidnew);
        }
        catch
        {
        }
    }
    public void setprice(int index)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        int i = dtCurrentTable.Rows.Count - 1;

        DropDownList stock = (DropDownList)Gridview1.Rows[i].FindControl("DropDownList4");
        string stockid = stock.SelectedValue;

        Bestlife.ERP.Entities.E_PurchaseOrder porder = Bestlife.ERP.BusinessRules.B_PurchaseOrder.getprice(stockid);
        TextBox box = (TextBox)Gridview1.Rows[i].Cells[7].FindControl("TextBox6");
        TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[4].FindControl("TextBox3");
        box3.Text = porder.Unitvalue.ToString();
        box.Text = porder.Retprice.ToString();



    }
    public void setpricenew(int index, string stockidnew)
    {

        Bestlife.ERP.Entities.E_PurchaseOrder porder = Bestlife.ERP.BusinessRules.B_PurchaseOrder.getprice(stockidnew);
        TextBox box = (TextBox)Gridview1.Rows[index].Cells[7].FindControl("TextBox6");
        TextBox box3 = (TextBox)Gridview1.Rows[index].Cells[4].FindControl("TextBox3");
        box3.Text = porder.Unitvalue.ToString();
        box.Text = porder.Retprice.ToString();



    }
    protected void TextBox4_TextChanged(object sender, EventArgs e)
    {

        try
        {
            indexx = int.Parse(ViewState["index"].ToString());
            TextBox box3 = (TextBox)Gridview1.Rows[indexx].Cells[3].FindControl("TextBox3");
            TextBox box4 = (TextBox)Gridview1.Rows[indexx].Cells[4].FindControl("TextBox4");
            decimal qty;
            decimal price;
            try
            {
                qty = decimal.Parse(box4.Text);
                price = decimal.Parse(box3.Text);
                ASPxLabel3.Visible = false;
                decimal tot = Bestlife.ERP.BusinessRules.B_PurchaseOrder.linetot(qty, price);
                string h = ViewState["total"].ToString();
                ViewState["total"] = decimal.Parse(ViewState["total"].ToString()) + tot;
                ASPxTextBox2.Text = ViewState["total"].ToString();
                TextBox box5 = (TextBox)Gridview1.Rows[indexx].Cells[5].FindControl("TextBox5");
                box5.Text = tot.ToString();
                add();
            }

            catch
            {
                qty = 0;
                price = 0;
                ASPxLabel3.Visible = true;
                ASPxLabel3.Text = "Invalid data type";

            }

        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }

    #region insert
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        string user = Session["username"].ToString();
        try
        {

            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DropDownList box1 = (DropDownList)Gridview1.Rows[i].Cells[1].FindControl("DropDownList3");
                    DropDownList box2 = (DropDownList)Gridview1.Rows[i].Cells[2].FindControl("DropDownList4");
                    TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("TextBox2");
                    TextBox box4 = (TextBox)Gridview1.Rows[i].Cells[4].FindControl("TextBox3");
                    TextBox box5 = (TextBox)Gridview1.Rows[i].Cells[5].FindControl("TextBox4");
                    TextBox box6 = (TextBox)Gridview1.Rows[i].Cells[6].FindControl("TextBox5");
                    TextBox box7 = (TextBox)Gridview1.Rows[i].Cells[7].FindControl("TextBox6");

                    TextBox boxoffice = (TextBox)Gridview1.Rows[dt.Rows.Count - 1].Cells[4].FindControl("TextBox3");
                    if (boxoffice.Text != "")
                    {
                        string pro = box1.Text;
                        string stock = box2.Text;
                        string description = box3.Text;
                        string unitvalue = box4.Text;
                        string qty = box5.Text;

                        Bestlife.ERP.Entities.E_PurchaseOrder order = new Bestlife.ERP.Entities.E_PurchaseOrder();
                        order.Or_ID1 = ASPxTextBox1.Text;
                        order.Stockid = stock;
                        order.Proname = pro;
                        order.Qty = decimal.Parse(qty);
                        order.Unitvalue = decimal.Parse(unitvalue);
                        order.Description = description;



                        result1 = Bestlife.ERP.BusinessRules.B_PurchaseOrder.insert(order, user);
                    }
                    else
                    {
                        ASPxLabel2.Text = "Purches order fail";
                        ASPxLabel2.Visible = true;
                    }

                }

                Bestlife.ERP.Entities.E_PurchaseOrder order1 = new Bestlife.ERP.Entities.E_PurchaseOrder();

                order1.Or_ID1 = ASPxTextBox1.Text;
                order1.Contrat = ASPxTextBox3.Text;
                order1.Due = ASPxTextBox4.Text;
                order1.Issu = ASPxDateEdit2.Date;
                order1.Supplier = DropDownList1.Text;
                order1.Supid = Bestlife.ERP.BusinessRules.B_PurchaseOrder.getsupid(DropDownList1.Text).ToString();
                Boolean result2;
                Boolean result3;
                Boolean result;
                if (result1 == true)
                {
                    result2 = Bestlife.ERP.BusinessRules.B_PurchaseOrder.insertorder(order1, user);

                    if (result2 == true)
                    {
                        result3 = Bestlife.ERP.BusinessRules.B_PurchaseOrder.insertsupp(order1, user);

                        Bestlife.ERP.Entities.E_PurchaseOrder total = new Bestlife.ERP.Entities.E_PurchaseOrder();
                        total.Total = decimal.Parse(ASPxTextBox2.Text);
                        total.Or_ID1 = ASPxTextBox1.Text;

                        if (result3 == true)
                        {
                            result = Bestlife.ERP.BusinessRules.B_PurchaseOrder.total(total, user);
                            if ((result1 == true) && (result2 == true) && (result3 == true) && (result == true))
                            {

                                ASPxLabel2.Text = "You have succesfully added your purchase order";
                                ASPxLabel2.Visible = true;
                                ASPxImage1.Visible = true;
                                ASPxButton1.Enabled = false;
                            }
                        }
                    }
                }
            }

        }
        catch
        {
            ASPxLabel2.Text = "Purches order fail";
            ASPxLabel2.Visible = true;
        }
    }
    #endregion

    protected void ASPxDateEdit2_DateChanged(object sender, EventArgs e)
    {

    }
    protected void ASPxButton3_Click(object sender, EventArgs e)
    {
        //pcLogin.Windows.Clear();


    }
    protected void ASPxButton3_Click1(object sender, EventArgs e)
    {
        String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
        Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class

        SqlCommand comm = new SqlCommand("SELECT  [Sto_ID],[Pro_Name],[Qty_in_stock],[Pro_ID] FROM [Tbl_Stock], connStr");
        SqlDataReader rdr = comm.ExecuteReader();
        Gridview1.DataSource = rdr;
        Gridview1.DataBind();

        rdr.Close();

    }
    protected void ASPxButton4_Click(object sender, EventArgs e)
    {

    }
    protected void ASPxButton5_Click(object sender, EventArgs e)
    {

    }
    public void setAlldata(int index)
    {
        //string proid = DropDownList9.Text;

        //Bestlife.ERP.Entities.product pro = new Bestlife.ERP.Entities.product();
        //pro = Bestlife.ERP.BusinessRules.B_PurchaseOrder.setalldata(proid);
        //subcatname.Text = pro.Sub_Cat_Name1;
        //catname.Text = pro.Cat_Name1;
        //brand.Text = pro.Brand;
        //Productid.Text = pro.Pro_ID;

    }
    protected void UpdateSupplyLed(object sender, EventArgs e)
    {
        CheckBox chkSample = sender as CheckBox;
        bool sample = chkSample.Checked;

        //GridViewRow row = chkSample.NamingContainer as GridViewRow;
        //TextBox txtId = row.FindControl("DropDownList3") as TextBox;
        //string id = txtId.Text;
    }
    public void add()
    {
        DataTable dt = (DataTable)ViewState["CurrentTable"];
        int rowIndex = dt.Rows.Count - 1;
        DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList3");
        DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList4");
        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox2");
        TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox3");
        TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox4");
        TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox5");
        TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox6");

        if (box4.Text == "" || box5.Text == "" || box6.Text == "")
        {

            ASPxLabel3.Visible = true;
            ASPxLabel3.Text = "You must enter a value for all required fields ";




        }
        else
        {
            setprice(0);
            AddNewRowToGrid();
            Session["addrow"] = 1;



        }

    }
    protected void Gridview1_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName.Equals("Delete"))
            {

                int i = Convert.ToInt32(e.CommandArgument);

                DataTable dt = (DataTable)ViewState["CurrentTable"];
                if (dt.Rows.Count > i)
                {
                    System.Data.DataRow dr = dt.NewRow();

                    dt.Rows[i].Delete();

                    Gridview1.DataSource = dt;
                    //for (int j = 0; j < dt.Rows.Count; j++)
                    //{
                    //    dt.Columns["RowNumber"].ToString();
                    //}



                    Gridview1.DataBind();

                    SetPreviousData();
                    ViewState["total"] = 0;
                    indexx = int.Parse(ViewState["index"].ToString());
                   
                    decimal qty;
                    decimal price;
                    for (int f = 0; f < dt.Rows.Count ; f++)
                    {
                TextBox box3 = (TextBox)Gridview1.Rows[f].Cells[3].FindControl("TextBox3");
                TextBox box4 = (TextBox)Gridview1.Rows[f].Cells[4].FindControl("TextBox4");
                qty = decimal.Parse(box4.Text);
                price = decimal.Parse(box3.Text);
                ASPxLabel3.Visible = false;
                decimal tot = Bestlife.ERP.BusinessRules.B_PurchaseOrder.linetot(qty, price);
               // string h = ViewState["total"].ToString();
                ViewState["total"] = decimal.Parse(ViewState["total"].ToString()) + tot;
                ASPxTextBox2.Text = ViewState["total"].ToString();
                TextBox box5 = (TextBox)Gridview1.Rows[indexx].Cells[5].FindControl("TextBox5");
                box5.Text = tot.ToString();
                    }


                }

            }
        }
        catch
        {
        }
    }
    protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void addnew_WindowCallback(object source, DevExpress.Web.ASPxPopupControl.PopupWindowCallbackArgs e)
    {

    }
    protected void addnew_Load(object sender, EventArgs e)
    {

    }
    protected void stockbutton0_Click(object sender, EventArgs e)
    {

    }
}//row adding





